package infoserver;

import Protocol.*;
import com.sun.org.apache.bcel.internal.generic.CPInstruction;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.ArrayList;
import javax.swing.*;

/**
 * Dit is een testclient applicatie om de tot nu toe geimplementeerde functies van de server te testen
 * @author Michael Stevens
 * @since 04-05-2012
 */
public class TestClient extends JFrame implements ActionListener{
    private Socket connection;
    //private DataOutputStream output;
    private ObjectOutputStream output;
    private ComProtocol cp;
    private JButton select, update, login, logout;
    private JTextField user, query;
    private JPasswordField pass;
    private JLabel label;
    private JTextArea text;
    private String username, password;
    private JPanel jp;
    
    public TestClient(){
        setTitle("Test Client");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(455, 350);        
        setResizable(false);
        //setLayout(new FlowLayout());
        jp = new JPanel();
        jp.setLayout(null);
        getContentPane().add(jp);
        select = new JButton("Select");  
        select.setBounds(365, 100, 75, 25);
        jp.add(select);
        
        label = new JLabel("Query : ");
        label.setBounds(10, 73, 70, 15);        
        jp.add(label);
        
        query = new JTextField(10);
        query.setBounds(90, 70, 265, 20);
        jp.add(query);
        query.setEnabled(false);
        
        update = new JButton("Update");
        update.setBounds(365, 130, 75, 25);
        jp.add(update);
        
        login = new JButton("Login");
        login.setBounds(200, 10, 75, 25);
        jp.add(login);
        
        logout = new JButton("Loguit");
        logout.setBounds(200, 40, 75, 25);
        jp.add(logout);
        select.setEnabled(false);
        update.setEnabled(false);
        logout.setEnabled(false);
        
        label = new JLabel("Username : ");
        label.setBounds(10, 14, 70, 15);        
        jp.add(label);
        
        user = new JTextField(10);
        user.setBounds(90, 13, 100, 20);
        jp.add(user);
        
        label = new JLabel("Password : ");
        label.setBounds(10, 45, 70, 15);
        jp.add(label);
        
        pass = new JPasswordField(10);
        pass.setBounds(90, 43, 100, 20);
        jp.add(pass);
        
        text = new JTextArea(7,20);
        text.setBounds(10, 100, 345, 210);
        text.setEditable(false);
        jp.add(text);
        
        select.addActionListener(this);
        update.addActionListener(this);
        login.addActionListener(this);
        logout.addActionListener(this);
        setVisible(true);
    }
    
    public static void main(String[] args) {
        TestClient t = new TestClient();   
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == select){
            try{
                String q = "";
                if(query.getText().equals("")){
                    text.setText("Geen query ingevoerd");
                }else{
                    q = query.getText();
                    cp = new ComProtocol("localhost", 5555);
                    ClientObject o = new ClientObject(username, password, cp.getSelectQuery(), q);   
                    cp.send(o);
                    ReturnObject ro = (ReturnObject) cp.recieve();
                    cp.close();
                    if(ro == null){
                        text.setText("Query fout");
                    }else{
                        if(ro.getMessage().equals("ok")){
                            System.out.println("query ok");
                            text.setText(verwerkReturnObject(ro));
                        }else{
                            text.setText("Geen resultaten gevonden");
                        }
                    }
                }
            }catch(Exception a){
                System.out.println(a.getMessage());
            }                   
        }else if(e.getSource() == update){
            try{
                String q = "";
                if(query.getText().equals("")){
                    text.setText("Geen query ingevoerd");
                }else{
                    q = query.getText();
                    cp = new ComProtocol("localhost", 5555);
                    ClientObject o = new ClientObject(username, password, cp.getUpdateQuery(), q);
                    cp.send(o);
                    ReturnObject ro = (ReturnObject) cp.recieve();
                    cp.close();
                    if(ro.getMessage().equals("ok")){
                        text.setText("Query gelukt");
                    }else{
                        text.setText("Query mislukt");
                    }
                }
            }catch(Exception a){
                System.out.println(a.getMessage());
            }            
        }else if(e.getSource() == login){
            try{                
                cp = new ComProtocol("localhost", 5555);
                ClientObject o = new ClientObject(user.getText(), pass.getText(), cp.getInloggen(), "");
                cp.send(o);
                ReturnObject ro = (ReturnObject) cp.recieve(); 
                cp.close();
                if(ro.getMessage().equals("ok")){
                    select.setEnabled(true);
                    update.setEnabled(true);
                    logout.setEnabled(true);
                    login.setEnabled(false);
                    username = user.getText();
                    password = pass.getText();
                    user.setText("");
                    pass.setText("");
                    user.setEnabled(false);
                    pass.setEnabled(false);
                    query.setEnabled(true);
                }else{
                    text.setText("Inloggen mislukt");
                }        
            }catch(Exception a){
                System.out.println(a.getMessage());
            }  
        }else if(e.getSource().equals(logout)){
            try{                
                cp = new ComProtocol("localhost", 5555);
                ClientObject o = new ClientObject(username, password, cp.getUitloggen(), "");
                cp.send(o);
                ReturnObject ro = (ReturnObject) cp.recieve();
                cp.close();
                if(ro.getMessage().equals("ok")){
                    select.setEnabled(false);
                    update.setEnabled(false);
                    logout.setEnabled(false);
                    login.setEnabled(true);
                    username = "";
                    password = "";
                    text.setText("");
                    user.setEnabled(true);
                    pass.setEnabled(true);
                    query.setText("");
                    query.setEnabled(false);
                }
            }catch(Exception a){
                System.out.println(a.getMessage());
            } 
        }
            
    }
    
    /**
    * Hier wordt het binnenkomende returnobject uitgelezen
    * @return de string met gegevens
    * @author Michael Stevens
    * @since 04-05-2012
    */
    public String verwerkReturnObject(ReturnObject ro){
        String result = "";
        for(String s :ro.getKolommen()){
            result += s + "  | ";
        }
        result += "\n";
        for(ArrayList a:ro.getRijen()){
            for(Object s : a){
                result += s.toString() + " | ";
            }
            result += "\n";
        }
        return result;
    }    
    
}
